<?php
define("EW_PAGE_ID", "report", TRUE); // Page ID
?>
<?php 
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg50.php" ?>
<?php include "db.php" ?>


<?php
$grp_x_Name = NULL;
$tmp_x_Name = NULL;
$grp_x_Amount = NULL;
$tmp_x_Amount = NULL;
$sx_Amount = array_pad(array(), 2, 0); // Array for SUM/AVERAGE
$grp_x_Lookup = NULL;
$tmp_x_Lookup = NULL;
$nCntRecs = array_pad(array(), 2, 0);
$bLvlBreak = array_pad(array(), 2, false);	
$sDbWhereMaster = "";
$sDbWhereDetail = "";

// Open connection to the database
$conn = phpmkr_db_connect(HOST, USER, PASS, DB, PORT);
?>
<?php @set_time_limit(999); ?>
<?php
if (!IsLoggedIn()) {
	ob_end_clean();
	header("Location: login.php");
	exit();
}
?>
<?php

// Initialize common variables
$x_Name = NULL;
$ox_Name = NULL;
$z_Name = NULL;
$ar_x_Name = NULL;
$ari_x_Name = NULL;
$x_NameList = NULL;
$x_NameChk = NULL;
$cbo_x_Name_js = NULL;
$x_Amount = NULL;
$ox_Amount = NULL;
$z_Amount = NULL;
$ar_x_Amount = NULL;
$ari_x_Amount = NULL;
$x_AmountList = NULL;
$x_AmountChk = NULL;
$cbo_x_Amount_js = NULL;
$x_Lookup = NULL;
$ox_Lookup = NULL;
$z_Lookup = NULL;
$ar_x_Lookup = NULL;
$ari_x_Lookup = NULL;
$x_LookupList = NULL;
$x_LookupChk = NULL;
$cbo_x_Lookup_js = NULL;
?>
<?php
$sExport = @$_GET["export"]; // Load export request
if ($sExport == "excel") {
	header('Content-Type: application/vnd.ms-excel');
	header('Content-Disposition: attachment; filename=' . ewTblVar .'.xls');
}
if ($sExport == "csv") {
	header('Content-Type: application/csv');
	header('Content-Disposition: attachment; filename=' . ewTblVar .'.csv');
}
?>
<p><span class="phpmaker">Report
<?php if ($sExport == "") { ?>
&nbsp;&nbsp;<a href="ActivitySummaryreport.php?export=excel&ID=<?php echo $parentid ?>&IPPSaleAmt=<?php echo $IPPSaleAmt ?>&XReadingAmt=<?php echo $XReadingAmt ?>" >Export to Excel</a>&nbsp;&nbsp;<a href="#" onclick="javascript:window.location.reload();">Refresh</a>
<?php } ?>
</span></p>
<form method="post">
<table class="ewReportTable" cellspacing="-1">
<?php
$sFilter = "";
if ($sDbWhereDetail <> "") {
	if ($sFilter <> "") $sFilter .= " AND ";
	$sFilter .= "(" . $sDbWhereDetail . ")";
}

// Set up SQL
$sSql = ewBuildSql(ewSqlGrpSelect, ewSqlGrpWhere, ewSqlGrpGroupBy, ewSqlGrpHaving, ewSqlGrpOrderBy, $sFilter, "");
//echo $sSql;
// Set up recordset

$rs = phpmkr_query($sSql,$conn) or die("Failed to execute query at line " . __LINE__ . ": " . phpmkr_error($conn) . '<br>SQL: ' . $sSql);
$nRecCount = 0;
$nCntRecs[0] = 0;
ChkLvlBreak();
if ($record = phpmkr_fetch_array($rs)) {
	$x_Lookup = $record["Lookup"];
}
while ($record) {
	$nRecCount++;

	// Show group headers
	if ($bLvlBreak[1]) { // Reset
?>
	<tr><td class="ewGroupField"><span class="phpmaker"></span></td>
	<td colspan=2 class="ewGroupName"><span class="phpmaker">
<?php echo $x_Lookup; ?>
</span></td></tr>
<?php
	}

	// Get detail records
	$sFilter = "";
	if ($sFilter <> "") $sFilter .= " AND ";
	if ((is_null($x_Lookup))) { 
		$sFilter .= "(`Lookup` IS NULL)";
	} else {
		$sFilter .= "(`Lookup` = '" . AdjustSql($x_Lookup) . "')";
	}
	if ($sDbWhereDetail <> "") {
		if ($sFilter <> "") $sFilter .= " AND ";
		$sFilter .= "(" . $sDbWhereDetail . ")";
	}

	// Set up SQL
	$sSql = ewBuildSql(ewSqlDtlSelect, ewSqlDtlWhere, ewSqlDtlGroupBy, ewSqlDtlHaving, ewSqlDtlOrderBy, $sFilter, "");
//echo $sSql;
	// Set up recordset
	$rsdtl = phpmkr_query($sSql,$conn) or die("Failed to execute query at line " . __LINE__ . ": " . phpmkr_error($conn) . '<br>SQL: ' . $sSql);
	$nDtlRecs = phpmkr_num_rows($rsdtl);

	// Init aggregate values
	if ($nDtlRecs > 0 ) {
		if ($nRecCount == 1) {
			$nCntRecs[0] = 0;
		}
		for ($i=1; $i<=1; $i++) {
			if ($bLvlBreak[$i]) { // Reset
				$nCntRecs[$i] = 0;
			}
		}
	}
	$nCntRecs[0] = $nCntRecs[0] + $nDtlRecs;
	$nCntRecs[1] = $nCntRecs[1] + $nDtlRecs;
?>
<?php
	$numOfDtl = 0;
	while ($dtl = phpmkr_fetch_array($rsdtl)) {
	if ($nRecCount == 1 && $numOfDtl == 0) {
		$sx_Amount[0] = 0;
	}
	if (($numOfDtl == 0) && ((is_null($x_Lookup)) Or (is_null($grp_x_Lookup)) Or $x_Lookup <> $grp_x_Lookup)) {
		$sx_Amount[1] = 0;
	}
		$x_Name = $dtl["Name"];
		$x_Amount = $dtl["Amount"];
		$sx_Amount[0] = $sx_Amount[0] + $x_Amount;
		$sx_Amount[1] = $sx_Amount[1] + $x_Amount;
?>
	<tr>
		<td style="width: 8px;"></td>
		<td><span class="phpmaker">
<?php echo $x_Name; ?>
</span></td>
		<td align="right"><span class="phpmaker">
<?php echo ew_FormatNumber($x_Amount,-1,0,-1,-2); ?>
</span></td>
	</tr>
<?php
	$numOfDtl++;
	}
	phpmkr_free_result($rsdtl);
	$rsdtl = NULL;

	// Save old group data
	$grp_x_Lookup = $x_Lookup;

	// Get next record
	$record = phpmkr_fetch_array($rs);
	if ($record) {
		$x_Lookup = $record["Lookup"];	
	} else {
		$nRecCount = 0; // EOF, force all level breaks
	}
	ChkLvlBreak();

	// Show footers
	if ($bLvlBreak[1]) 
	{
?>	

	<tr>
		<td class="ewGroupAggregate" colspan="2" align="right"><span class="phpmaker">Total <?php echo $grp_x_Lookup;?></span></td>
		<td  class="ewGroupAggregate"  align="right"><span class="phpmaker"><?php $x_Amount = $sx_Amount[1] ?>
<?php echo ew_FormatNumber($x_Amount,-1,0,-1,-2); ?>
</span></td>
	</tr>

<?php
	}
}

// Close recordset and connection
phpmkr_free_result($rs);
$rs = NULL;
?>

    <tr>
      <td colspan="2" align="right" class="ewGroupAggregate"><span class="phpmaker">Total IPP Sales</span></td>
      <td align="right"  class="ewGroupAggregate" ><span class="phpmaker"><?php echo ew_FormatNumber($IPPSaleAmt,-1,0,-1,-2); ?></span></td>
    </tr>
    <tr>
      <td  colspan="2" align="right" class="ewGroupAggregate"><span class="phpmaker">X Reading</span></td>
      <td align="right"  class="ewGroupAggregate" ><span class="phpmaker"><?php echo ew_FormatNumber($XReadingAmt,-1,0,-1,-2); ?></span></td>
    </tr>


	<tr><td colspan=3 class="ewGrandSummary"><span class="phpmaker">Grand Total (<?php echo
$nCntRecs[0]+2; ?> Detail Records
	)</span></td></tr>
	<tr>
		<td class="ewGroupAggregate" colspan="2" align="right"><span class="phpmaker">Grand Totals:</span></td>
		<td  class="ewGroupAggregate" align="right"><span class="phpmaker"><?php $x_Amount = $sx_Amount[0]+$XReadingAmt+$IPPSaleAmt ?>
<?php echo ew_FormatNumber($x_Amount,-1,0,-1,-2); ?>
</span></td>
	</tr>
	<tr><td colspan=3><span class="phpmaker">&nbsp;<br></span></td></tr>
</table>
</form>
<?php

// Check level break
function ChkLvlBreak()
{
	global $bLvlBreak;
	global $nRecCount;
	$bLvlBreak[1] = false;
	if ($nRecCount == 0) { // Start or end of recordset
		$bLvlBreak[1] = true;
	} else { 
		if ((is_null($GLOBALS["x_Lookup"])) Or (is_null($GLOBALS["grp_x_Lookup"])) Or $GLOBALS["x_Lookup"] <> $GLOBALS["grp_x_Lookup"]) {
			$bLvlBreak[1] = true;
		}
	}
}
?>

